<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:r="clr-namespace:Ricciolo.Controls"
    xmlns:s="clr-namespace:System;assembly=mscorlib">

  <r:LevelToIndentConverter x:Key="LevelToIndentConverter"/>

  <Style x:Key="ExpandCollapseToggleStyle"
           TargetType="{x:Type ToggleButton}">
    <Setter Property="Focusable"
            Value="False"/>
    <Setter Property="Width"
            Value="19"/>
    <Setter Property="Height"
            Value="13"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
          <Border Width="19"
                  Height="13"
                  Background="Transparent">
            <Border Width="9"
                    Height="9"
                    BorderThickness="1"
                    BorderBrush="#FF8E997D"
                    CornerRadius="1"
                    SnapsToDevicePixels="true">
              <Border.Background>
                <LinearGradientBrush StartPoint="0,0"
                                     EndPoint="1,1">
                  <LinearGradientBrush.GradientStops>
                    <GradientStop Color="White"
                                  Offset=".4"/>
                    <GradientStop Color="#FFC3BAAA"
                                  Offset="1"/>
                  </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
              </Border.Background>
              <Path x:Name="ExpandPath"
                    Margin="1,1,1,1"
                    Fill="Black"
                    Data="M 0 2 L 0 3 L 2 3 L 2 5 L 3 5 L 3 3 L 5 3 L 5 2 L 3 2 L 3 0 L 2 0 L 2 2 Z"/>
            </Border>
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsChecked"
                     Value="True">
              <Setter Property="Data"
                      TargetName="ExpandPath"
                      Value="M 0 2 L 0 3 L 5 3 L 5 2 Z"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style TargetType="{x:Type r:TreeListViewItem}">

    <Setter Property="Background"
            Value="Transparent"/>
    <Setter Property="VerticalContentAlignment"
            Value="Center"/>
    <Setter Property="Control.Template">
      <Setter.Value>
        <ControlTemplate TargetType="r:TreeListViewItem">
          <StackPanel>
            <Border Name="Bd"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Padding="{TemplateBinding Padding}"
                    SnapsToDevicePixels="true">
              <r:TreeGridViewRowPresenter x:Name="PART_Header"
                FirstColumnIndent="{Binding Level,Converter={StaticResource LevelToIndentConverter},RelativeSource={RelativeSource AncestorType={x:Type r:TreeListViewItem}},ConverterParameter=19}"
                Content="{TemplateBinding Header}" 
                Columns="{Binding Path=Columns,RelativeSource={RelativeSource AncestorType={x:Type r:TreeListView}}}">
                <r:TreeGridViewRowPresenter.Expander>
                  <Grid>
                    <ToggleButton x:Name="Expander" Height="19" Width="19"
                                      IsChecked="{Binding Path=IsExpanded,RelativeSource={RelativeSource AncestorType={x:Type r:TreeListViewItem}}}"
                                      Style="{StaticResource ExpandCollapseToggleStyle}"
                                      ClickMode="Press"/>
                  </Grid>
                </r:TreeGridViewRowPresenter.Expander>
              </r:TreeGridViewRowPresenter>
            </Border>

            <ItemsPresenter Name="ItemsHost" />
          </StackPanel>
          <ControlTemplate.Triggers>
            <Trigger Property="TreeViewItem.IsExpanded">
              <Setter Property="UIElement.Visibility" TargetName="ItemsHost" Value="{x:Static Visibility.Collapsed}" />
              <Trigger.Value>
                <s:Boolean>False</s:Boolean>
              </Trigger.Value>
            </Trigger>
            <Trigger Property="ItemsControl.HasItems">
              <Setter Property="UIElement.Visibility" TargetName="Expander" Value="{x:Static Visibility.Hidden}" />
              <Trigger.Value>
                <s:Boolean>False</s:Boolean>
              </Trigger.Value>
            </Trigger>

            <Trigger Property="TreeViewItem.IsSelected" Value="true">
              <Setter TargetName="Bd"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
              <Setter TargetName="Bd" Property="TextElement.Foreground"
                      Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
            </Trigger>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="TreeViewItem.IsSelected"
                           Value="true"/>
                <Condition Property="Selector.IsSelectionActive"
                           Value="false"/>
              </MultiTrigger.Conditions>
              <Setter TargetName="Bd"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
              <Setter TargetName="Bd" Property="TextElement.Foreground"
                      Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            </MultiTrigger>
            <Trigger Property="IsEnabled"
                     Value="false">
              <Setter TargetName="Bd" Property="TextElement.Foreground"
                      Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
          </ControlTemplate.Triggers>

        </ControlTemplate>
      </Setter.Value>
    </Setter>

  </Style>

  <Style x:Key="TreeViewItemFocusVisual">
    <Setter Property="Control.Template">
      <Setter.Value>
        <ControlTemplate>
          <Rectangle />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="scroll">
    <Setter Property="UIElement.Focusable">
      <Setter.Value>
        <s:Boolean>False</s:Boolean>
      </Setter.Value>
    </Setter>
    <Setter Property="Control.Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ScrollViewer}">
          <Grid Background="{TemplateBinding Background}"
          SnapsToDevicePixels="true">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <DockPanel Margin="{TemplateBinding Padding}">
              <ScrollViewer DockPanel.Dock="Top"
                            HorizontalScrollBarVisibility="Hidden"
                            VerticalScrollBarVisibility="Hidden"
                            Focusable="false">

                <GridViewHeaderRowPresenter Columns="{Binding Path=Columns,RelativeSource={RelativeSource AncestorType={x:Type r:TreeListView}}}"
                                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
              </ScrollViewer>
              <ScrollContentPresenter Name="PART_ScrollContentPresenter"
                                      KeyboardNavigation.DirectionalNavigation="Local"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      CanContentScroll="{TemplateBinding CanContentScroll}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            </DockPanel>
            <ScrollBar Name="PART_HorizontalScrollBar"
                                               Orientation="Horizontal"
                                               Grid.Row="1"
                                               Minimum="0.0"
                                               Maximum="{TemplateBinding ScrollableWidth}"
                                               ViewportSize="{TemplateBinding ViewportWidth}"
                                               Value="{Binding Path=HorizontalOffset,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}"
                                               Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                               Cursor="Arrow"/>
            <ScrollBar Name="PART_VerticalScrollBar"
                       Orientation="Vertical"
                       Grid.Column="1"
                       Minimum="0.0"
                       Maximum="{TemplateBinding ScrollableHeight}"
                       ViewportSize="{TemplateBinding ViewportHeight}"
                       Value="{Binding Path=VerticalOffset,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}"
                       Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                       Cursor="Arrow"/>
            <DockPanel Grid.Column="1"
                       Grid.Row="1"
                       Background="{Binding Path=Background,ElementName=PART_VerticalScrollBar}"
                       LastChildFill="false">
              <Rectangle DockPanel.Dock="Left"
                         Width="1"
                         Fill="White"
                         Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
              <Rectangle DockPanel.Dock="Top"
                         Height="1"
                         Fill="White"
                         Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
            </DockPanel>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <SolidColorBrush x:Key="ListBorder"
                     Color="#FFA5ACB2"/>
  
  <Style TargetType="{x:Type r:TreeListView}">

    <Setter Property="Background"
                Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="BorderBrush"
            Value="{StaticResource ListBorder}"/>
    <Setter Property="BorderThickness"
            Value="1"/>
    <Setter Property="Foreground"
            Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
            Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
            Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll"
            Value="true"/>
    <Setter Property="Control.Template">
      <Setter.Value>
        <ControlTemplate TargetType="r:TreeListView">
          <Border Name="Bd"
              Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}"
              SnapsToDevicePixels="true">
            <ScrollViewer Style="{StaticResource scroll}" Background="{TemplateBinding Background}"
              Focusable="false"
              CanContentScroll="false"
              HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
              VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
              Padding="{TemplateBinding Padding}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
              <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </ScrollViewer>
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsGrouping"
                     Value="true">
              <Setter Property="ScrollViewer.CanContentScroll"
                      Value="false"/>
            </Trigger>
            <Trigger Property="IsEnabled"
                     Value="false">
              <Setter TargetName="Bd"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

</ResourceDictionary>